#!/bin/sh

SRC_DIR=`dirname $0`
source $SRC_DIR/env.sh

DBNAME=$1
if [ ! -f "${DBPATH}/${DBNAME}.ns" ]; then
  echo "no database found"
  exit -1
fi

INDEX=0
LAST_FILE=""
while [ -f "${DBPATH}/${DBNAME}.${INDEX}" ]
do
  LAST_FILE="${DBPATH}/${DBNAME}.${INDEX}"
  INDEX=`expr $INDEX + 1`
done

LAST_MOD=`stat -c "%Y" $LAST_FILE`
NOW=`date "+%s"`
SINCE=`expr $NOW - $LAST_MOD`
if [ $SINCE -le 1800 ]; then
  echo "last file $LAST_FILE is updated in last 30 minuites. will be create next file."
  NEXT_FILE="${DBPATH}/${DBNAME}.${INDEX}"
  echo "allocate $NEXT_FILE ..."
  $SRC_DIR/allocate.sh $DBNAME $INDEX
  echo "done."
fi

